package com.evl.api.trainOrder.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.evl.api.payOrder.service.IPayTrainOrderService;
import com.evl.api.trainOrder.service.IApiTrainOrderService;
import com.evl.api.trainOrder.vo.InvailOrderRQ;
import com.evl.api.trainOrder.vo.OrderPayInfoRQ;
import com.evl.api.trainOrder.vo.PassengerRS;
import com.evl.api.trainOrder.vo.TrainOrderCreateRQ;
import com.evl.api.trainOrder.vo.TrainOrderSearchRQ;
import com.evl.api.trainOrder.vo.TrainOrderSearchRS;
import com.evl.api.trainOrder.vo.TrainTicketSearchRQ;
import com.evl.common.controller.BaseController;
import com.evl.common.persistence.model.TrainOrder;
import com.evl.constant.ErrorContant;
import com.evl.constant.TrainTicketStatus;
import com.evl.core.vo.BaseResultVo;
import com.evl.core.vo.JsonReturnHelper;
import com.evl.utils.ObjectUtil;
import com.evl.utils.filter.ListFilter;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 
 * @author dangsheng
 * 订单操作
 */
@Controller
@RequestMapping("/api/trainOrder")
@Api(value = "订单操作")
public class ApiTrainOrderController extends BaseController {
	
	@Resource
	IApiTrainOrderService apiTrainOrderService;
	@Resource
	IPayTrainOrderService iPayTrainOrderService;
	
    @RequestMapping("/create")
    @ApiOperation(value="生成订单",httpMethod = "POST", response = BaseResultVo.class, notes = "订单信息")
    @ResponseBody
	public BaseResultVo trainOrder(@ApiParam(required = true) @RequestBody TrainOrderCreateRQ trainOrderVo) {
    	Map resoutMap;
    	TrainOrder trainOrder;
    	try {
    		trainOrder = apiTrainOrderService.trainOrder(trainOrderVo);
		} catch (Exception e) {
			return JsonReturnHelper.getFailWithError(e.getMessage(), ErrorContant.TICKET_STATUS_TRAIN_ORDER_ERROR_CODE);
		}
    	
		//调用车票订单支付接口
		try{
			resoutMap = iPayTrainOrderService.order(trainOrder.getId()+"",trainOrderVo.getFormId());
		}catch (Exception e1) {
			return JsonReturnHelper.getFailWithError(e1.getMessage(), ErrorContant.TICKET_STATUS_TRAIN_ORDER_PAY_ERROR_CODE);
		}
		
		if(null != trainOrder){
			resoutMap.put("orderNo", trainOrder.getOrderNo());
		}
		return JsonReturnHelper.getSuccessWithData(resoutMap);
	}

    @RequestMapping("/search")
    @ApiOperation(value="订单查询",httpMethod = "POST", response = BaseResultVo.class, notes = "订单查询新")
    @ResponseBody
	public BaseResultVo searchTrainOrder(@ApiParam(required = true) @RequestBody TrainOrderSearchRQ vo) {
    	//当前用户不能为空
    	if(StringUtils.isEmpty(vo.getCreateId())){
    		return JsonReturnHelper.getFailWithError(ErrorContant.TICKET_STATUS_TRAIN_ORDER_SEARCH_MSG, ErrorContant.TICKET_STATUS_TRAIN_ORDER_SEARCH_CODE);
    	}
    	List<TrainOrderSearchRS> list=apiTrainOrderService.searchTrainOrder(vo);
    	return JsonReturnHelper.getSuccessWithData(list);
	}
    
    @RequestMapping("/searchOne")
    @ApiOperation(value="订单查询",httpMethod = "POST", response = BaseResultVo.class, notes = "订单查询信息")
    @ResponseBody
	public BaseResultVo searchOneTrainOrder(@ApiParam(required = true) @RequestBody TrainOrderSearchRQ rq) {
    	//当前用户不能为空
    	if(StringUtils.isEmpty(rq.getCreateId())){
    		return JsonReturnHelper.getFailWithError(ErrorContant.TICKET_STATUS_TRAIN_ORDER_SEARCH_MSG, ErrorContant.TICKET_STATUS_TRAIN_ORDER_SEARCH_CODE);
    	}
    	//查询单条数据需要orderId或者OrderNo确定唯一性
    	if(StringUtils.isEmpty(rq.getId()) && StringUtils.isEmpty(rq.getOrderNo())){
    		return JsonReturnHelper.getFailWithError(ErrorContant.TICKET_STATUS_TRAIN_ORDER_SEARCH_ONE_MSG, ErrorContant.TICKET_STATUS_TRAIN_ORDER_SEARCH_ONE_CODE);
    	}
    	return apiTrainOrderService.searchOrderTicket(rq);
	}

    @RequestMapping("/payOrder")
    @ApiOperation(value="订单支付",httpMethod = "POST", response = BaseResultVo.class, notes = "订单支付")
    @ResponseBody
	public BaseResultVo payTrainOrder(@ApiParam(required = true) @RequestBody OrderPayInfoRQ orderPayInfo) {
    	Map resoutMap = null;
		try {
			resoutMap = iPayTrainOrderService.order(orderPayInfo.getOrderId()+"",orderPayInfo.getFormId());
		} catch (Exception e) {
			JsonReturnHelper.getFailWithError(e.getMessage(), ErrorContant.TICKET_STATUS_TRAIN_ORDER_PAY_ERROR_CODE);
		}
    	return JsonReturnHelper.getSuccessWithData(resoutMap);
	}
    
	@RequestMapping("/refundTicketApply")
    @ApiOperation(value="退票申请",httpMethod = "POST", response = BaseResultVo.class, notes = "申请退票")
    @ResponseBody
	public BaseResultVo refundTicketApply(@ApiParam(required = true) @RequestBody TrainTicketSearchRQ paramVo) {
		if(paramVo.getTicketId() == null){
			return JsonReturnHelper.getFailWithError(ErrorContant.TRAIN_TICKET_PARAM_MSG, ErrorContant.TRAIN_TICKET_PARAM_CODE);
		}
		return apiTrainOrderService.refundTicketApply(paramVo);
	}
	
	@RequestMapping("/refundTicket")
    @ApiOperation(value="退票",httpMethod = "POST", response = BaseResultVo.class, notes = "退票")
    @ResponseBody
	public BaseResultVo refundTicket(@ApiParam(required = true) String ticketId) {
		return apiTrainOrderService.refundTicket(ticketId);
	}
	
    @RequestMapping("/searchRefundTicket")
    @ApiOperation(value="订单查询",httpMethod = "POST", response = BaseResultVo.class, notes = "订单查询信息")
    @ResponseBody
	public BaseResultVo searchRefundTicket(@ApiParam(required = true) @RequestBody TrainOrderSearchRQ rq) {
    	return apiTrainOrderService.searchOrderTicket(rq);
	}
	
	
	@RequestMapping("/invailOrder")
    @ApiOperation(value="取消订单",httpMethod = "POST", response = BaseResultVo.class, notes = "订单取消")
    @ResponseBody
	public BaseResultVo refundTicket(@ApiParam(required = true) @RequestBody InvailOrderRQ invailOrderRQ) {
		return apiTrainOrderService.invailOrder(invailOrderRQ.getId()+"");
	}
	
	
	
    
    
}
